package com.example.chasshelper;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.viewpager2.widget.ViewPager2;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.example.chasshelper.dao.ClassroomDao;
import com.example.chasshelper.dao.CourseDao;
import com.example.chasshelper.dao.MessageDao;
import com.example.chasshelper.dao.RegistrationDao;
import com.example.chasshelper.dao.SolutionDao;
import com.example.chasshelper.dao.StudentDao;
import com.example.chasshelper.dao.TeacherDao;
import com.example.chasshelper.dao.TestDao;
import com.example.chasshelper.model.Student;
import com.example.chasshelper.model.Teacher;
import com.example.chasshelper.utils.DaoUtils;

import java.util.ArrayList;
import java.util.List;

public class EditPasswordActivity extends AppCompatActivity implements View.OnClickListener {
    private static final String TAG = EditPasswordActivity.class.getSimpleName();
    private final Activity mContext = EditPasswordActivity.this;
    //控件
    private ImageView iv_back;
    private Button btn_save;
    private EditText et_oldPassword;
    private EditText et_newPassword;
    private EditText et_check_newPassword;
    //dao
    private TeacherDao teacherDao= DaoUtils.getTeacherDao();
    private StudentDao studentDao=DaoUtils.getStudentDao();
    private CourseDao courseDao=DaoUtils.getCourseDao();
    private ClassroomDao classroomDao=DaoUtils.getClassroomDao();
    private RegistrationDao registrationDao=DaoUtils.getRegistrationDao();
    private SolutionDao solutionDao = DaoUtils.getSolutionDao();
    private MessageDao messageDao = DaoUtils.getMessageDao();
    private TestDao testDao = DaoUtils.getTestDao();
    //数据
    private Teacher teacher;
    private Student student;
    private String identity;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_edit_password);
        initUI();
        initData();
    }
    /*
     * 界面初始化
     * */
    private void initUI() {
        //绑定控件
        iv_back = findViewById(R.id.iv_back);
        btn_save = findViewById(R.id.btn_save);
        et_oldPassword = findViewById(R.id.et_oldPassword);
        et_newPassword = findViewById(R.id.et_newPassword);
        et_check_newPassword = findViewById(R.id.et_check_newPassword);
        //点击事件
        iv_back.setOnClickListener(this);
        btn_save.setOnClickListener(this);
    }

    /*
     * 数据初始化
     * */
    private void initData() {
        identity = (String) getIntent().getSerializableExtra("identity");
        if (identity.equals("教师")) {
            teacher = (Teacher) getIntent().getSerializableExtra("teacher");
        } else {
            student = (Student) getIntent().getSerializableExtra("student");
        }
    }

    //点击事件
    @Override
    public void onClick(View view) {
        Intent intent = new Intent();
        String userOldPassword = et_oldPassword.getText().toString();
        String userNewPassword = et_newPassword.getText().toString();
        String userCheckNewPassword = et_check_newPassword.getText().toString();
        switch (view.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.btn_save:
                if (userOldPassword.trim().length() == 0 || userNewPassword.trim().length() == 0 || userCheckNewPassword.trim().length() == 0) {
                    Toast.makeText(mContext, "输入不能为空", Toast.LENGTH_SHORT).show();
                    return;
                }

                if (!userNewPassword.equals(userCheckNewPassword)) {//判断两次新密码是否一致
                    Toast.makeText(mContext, "两次输入的密码不一致", Toast.LENGTH_SHORT).show();
                    return;
                }

                if (identity.equals("教师")) {
                    if (!teacher.getTeacherPassword().equals(userOldPassword)) {
                        Toast.makeText(mContext, "密码错误", Toast.LENGTH_SHORT).show();
                        return;
                    }
                    teacher.setTeacherPassword(userNewPassword);
                    teacherDao.changePassword(teacher);
                } else {
                    if (!student.getStudentPassword().equals(userOldPassword)) {
                        Toast.makeText(mContext, "密码错误", Toast.LENGTH_SHORT).show();
                        return;
                    }
                    student.setStudentPassword(userNewPassword);
                    studentDao.changePassword(student);
                }
                Toast.makeText(mContext, "修改密码成功", Toast.LENGTH_SHORT).show();
                finish();
                break;
        }

       }
}